package com.example.hibernate.demo.config;

import com.example.hibernate.demo.entity.*;
import com.example.hibernate.demo.repository.*;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@RequiredArgsConstructor
public class DataInitializer implements CommandLineRunner {

    private final DepartmentRepository departmentRepository;
    private final EmployeeRepository employeeRepository;
    private final ProjectRepository projectRepository;
    private final ProductRepository productRepository;
    private final OrderRepository orderRepository;
    private final PaymentRepository paymentRepository;
    private final DataInitializerPart2 dataInitializerPart2;

    @Override
    @Transactional
    public void run(String... args) {
        // 只有当数据库为空时才初始化数据
        if (departmentRepository.count() > 0) {
            return;
        }

        initDepartmentsAndEmployees();
        dataInitializerPart2.initProjectsAndProducts();
        dataInitializerPart2.initOrdersAndPayments();
    }
    
    private void initDepartmentsAndEmployees() {
        // 创建部门
        Department hrDepartment = Department.builder()
                .name("Human Resources")
                .description("Responsible for recruiting, employee relations, and benefits")
                .build();

        Department itDepartment = Department.builder()
                .name("Information Technology")
                .description("Responsible for technical infrastructure and software development")
                .build();

        Department financeDepartment = Department.builder()
                .name("Finance")
                .description("Responsible for financial planning and accounting")
                .build();

        departmentRepository.saveAll(Arrays.asList(hrDepartment, itDepartment, financeDepartment));

        // 创建员工
        Employee john = Employee.builder()
                .firstName("John")
                .lastName("Doe")
                .email("john.doe@example.com")
                .salary(new BigDecimal("75000.00"))
                .status(Employee.EmployeeStatus.ACTIVE)
                .department(hrDepartment)
                .build();

        Employee jane = Employee.builder()
                .firstName("Jane")
                .lastName("Smith")
                .email("jane.smith@example.com")
                .salary(new BigDecimal("85000.00"))
                .status(Employee.EmployeeStatus.ACTIVE)
                .department(itDepartment)
                .build();

        Employee bob = Employee.builder()
                .firstName("Bob")
                .lastName("Johnson")
                .email("bob.johnson@example.com")
                .salary(new BigDecimal("65000.00"))
                .status(Employee.EmployeeStatus.ON_LEAVE)
                .department(financeDepartment)
                .build();

        employeeRepository.saveAll(Arrays.asList(john, jane, bob));

        // 创建员工详细信息
        EmployeeDetail johnDetail = EmployeeDetail.builder()
                .address("123 Main St, Anytown")
                .phoneNumber("555-1234")
                .birthDate(LocalDate.of(1985, 5, 15))
                .hireDate(LocalDate.of(2020, 3, 10))
                .emergencyContact(new EmployeeDetail.EmergencyContact("Mary Doe", "Spouse", "555-5678"))
                .employee(john)
                .build();
        john.setEmployeeDetail(johnDetail);

        EmployeeDetail janeDetail = EmployeeDetail.builder()
                .address("456 Oak St, Somewhere")
                .phoneNumber("555-4321")
                .birthDate(LocalDate.of(1990, 8, 22))
                .hireDate(LocalDate.of(2019, 7, 15))
                .emergencyContact(new EmployeeDetail.EmergencyContact("Tom Smith", "Spouse", "555-8765"))
                .employee(jane)
                .build();
        jane.setEmployeeDetail(janeDetail);

        employeeRepository.saveAll(Arrays.asList(john, jane));
    }
}